package hikari;

import com.gooddies.wiring.Wiring;
import hikari.gui.MainWindow;
import hikari.gui.SplashScreen;
import hikari.utils.PathToIrrlicht;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @author Dmitry
 */
public class Hikari {

    private static boolean debugIrrlicht = false;
    private static SplashScreen splashScreen;

    private static void setWindowsLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Hikari.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void loadLibraries() {
        String libDirectory;
        if (!debugIrrlicht) {
            libDirectory = new File(PathToIrrlicht.pathToIrrlichtLibrary).getParent();
        } else {
            libDirectory = new File(PathToIrrlicht.pathToDebugIrrlichtLibrary).getParent();
        }
        try {
            System.load(new File(libDirectory, "cg.dll").getAbsolutePath());
            System.load(new File(libDirectory, "cgD3D9.dll").getAbsolutePath());
            System.load(new File(libDirectory, "cgD3D10.dll").getAbsolutePath());
            System.load(new File(libDirectory, "cgD3D11.dll").getAbsolutePath());
            System.load(new File(libDirectory, "cgGL.dll").getAbsolutePath());
            System.load(new File(libDirectory, "Irrlicht.dll").getAbsolutePath());
            System.load(new File(PathToIrrlicht.pathToJirrlichtLibrary).getAbsolutePath());
        } catch (Error ex) {
            String message = "Cannot download dynamic library. Library is missing, or you have 64bit java, but hikari requires 32bit version.\nOs message:\n" + ex.getMessage();
            JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                splashScreen = new SplashScreen();
                splashScreen.setLocationRelativeTo(null);
                splashScreen.setVisible(true);
                splashScreen.repaint();
            }
        });
        loadLibraries();
        setWindowsLookAndFeel();
        initWiring();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame window = (JFrame) Wiring.getComponent(MainWindow.class);
                splashScreen.setVisible(false);
                splashScreen.dispose();
                window.setVisible(true);
            }
        });

    }

    private static void initWiring() {
        Wiring.init().defaultInit(Hikari.class);
    }
}
